require "ISUI/ISPanelJoypad"

QNW_TS_TaskTabPanel = ISPanelJoypad:derive("QNW_TS_TaskWindow")

local this = QNW_TS_TaskTabPanel
local FONT_HGT_SMALL = getTextManager():getFontHeight(UIFont.NewSmall)
local FONT_HGT_MEDIUM = getTextManager():getFontHeight(UIFont.NewMedium)
local FONT_HGT_LARGE = getTextManager():getFontHeight(UIFont.NewLarge)

function QNW_TS_TaskTabPanel:new(_x, _y, _width, _height, _player)
    local o = ISCollapsableWindow:new(_x, _y, _width, _height)
    setmetatable(o, self)
    self.__index = self
    o:noBackground()
    o.player = _player
    o.anchorRight = true
    o.anchorBottom = true
    o.anchorTop = true
    o.anchorLeft = true
    return o
end

local btnComplete = getText("IGUI_QNW_TaskSystem_Window_Complete")
local btnAbandon = getText("IGUI_QNW_TaskSystem_Window_Abandon")
local btnCompleteWid = getTextManager():MeasureStringX(UIFont.NewSmall, btnComplete) + 10
local btnCompleteHgt = getTextManager():MeasureStringY(UIFont.NewSmall, btnComplete) + 10
local btnAbandonWid = getTextManager():MeasureStringX(UIFont.NewSmall, btnAbandon) + 10
local btnAbandonHgt = getTextManager():MeasureStringY(UIFont.NewSmall, btnAbandon) + 10

function QNW_TS_TaskTabPanel:createChildren()
    ISPanelJoypad.createChildren(self)

    self.btnComplete = ISButton:new(self.width - btnCompleteWid - 5, self.height - btnCompleteHgt - 5, btnCompleteWid, btnCompleteHgt, btnComplete, self, this.onClick)
    self.btnComplete.anchorRight = true
    self.btnComplete.anchorBottom = true
    self.btnComplete.anchorTop = false
    self.btnComplete.anchorLeft = false
    self.btnComplete:initialise()
    self.btnComplete:instantiate()
    self.btnComplete.borderColor = { r = 1, g = 1, b = 1, a = 0.1 }
    self.btnComplete.enable = false
    self.btnComplete.internal = "Complete"
    self.btnComplete.anchorRight = true
    self.btnComplete.anchorBottom = true
    self:addChild(self.btnComplete)

    self.btnAbandon = ISButton:new(self.width - btnCompleteWid - btnAbandonWid - 10, self.height - btnCompleteHgt - 5, btnAbandonWid, btnAbandonHgt, btnAbandon, self, this.onClick)
    self.btnAbandon.anchorRight = true
    self.btnAbandon.anchorBottom = true
    self.btnAbandon.anchorTop = false
    self.btnAbandon.anchorLeft = false
    self.btnAbandon:initialise()
    self.btnAbandon:instantiate()
    self.btnAbandon.borderColor = { r = 1, g = 1, b = 1, a = 0.1 }
    self.btnAbandon.enable = false
    self.btnAbandon.internal = "Abandon"
    self:addChild(self.btnAbandon)

    self.datas = ISScrollingListBox:new(0, 0, self.width, self.height - btnCompleteHgt - 10)
    self.datas.anchorRight = true
    self.datas.anchorBottom = true
    self.datas:initialise()
    self.datas:instantiate()
    self.datas.itemheight = FONT_HGT_SMALL * 2 + FONT_HGT_MEDIUM + FONT_HGT_LARGE + 25
    self.datas.selected = 0
    self.datas.joypadParent = self
    self.datas.font = UIFont.NewMedium
    self.datas.doDrawItem = self.drawDatas
    self.datas.drawBorder = true
    self:addChild(self.datas)
end

function QNW_TS_TaskTabPanel:initList(_list)
    for k, v in pairs(_list) do
        self.datas:addItem(k, v)
    end
    table.sort(self.datas.items, function(a, b)
        a = a.item
        b = b.item
        if a:isComplete() and not b:isComplete() then
            return true
        end
        if not a:isComplete() and b:isComplete() then
            return false
        end
        if a.taskCategory.Type == b.taskCategory.Type then
            return string.sort(a.name, b.name)
        end
        return string.sort(a.taskCategory.name, b.taskCategory.name)
    end)
end

function QNW_TS_TaskTabPanel:onClick(_button)
    local task = self.datas.items[self.datas.selected].item
    if _button.internal == "Complete" and task:isComplete() then
        task:complete()
        self.parent.parent:refresh()
    end
    if _button.internal == "Abandon" and task:isAbandon() then
        task:abandon()
        self.parent.parent:refresh()
    end
end

function QNW_TS_TaskTabPanel:update()
    if self.datas.selected > 0 then
        local task = self.datas.items[self.datas.selected].item
        self.btnComplete.enable = task:isComplete()
        self.btnAbandon.enable = task:isAbandon()
    else
        self.btnComplete.enable = false
        self.btnAbandon.enable = false
    end
end

function QNW_TS_TaskTabPanel:drawDatas(_y, _item, _alt)
    if _y + self:getYScroll() + self.itemheight < 0 or _y + self:getYScroll() >= self.height then
        return _y + self.itemheight
    end

    local task = _item.item

    if self.selected == _item.index then
        self:drawRect(0, (_y), self:getWidth(), self.itemheight, 0.3, 1, 1, 0)
    else
        if task:isComplete() then
            self:drawRect(0, (_y), self:getWidth(), self.itemheight, 0.3, 0, 0.6, 0.4)
        else
            self:drawRect(0, (_y), self:getWidth(), self.itemheight, 0.3, 1, 1, 1)
        end
    end
    self:drawRectBorder(0, (_y), self:getWidth(), self.itemheight, 0.5, 1, 1, 1)
    self:drawText(task.taskCategory.name .. "-" .. task.name, 5, _y + 5, 1, 1, 1, 1, UIFont.NewLarge)
    self:drawText(task.desc, 5, _y + FONT_HGT_LARGE + 5, 1, 1, 1, 1, UIFont.FONT_HGT_MEDIUM)
    local time = getText("IGUI_QNW_TaskSystem_Window_Time") .. task:getTime()
    local progress = getText("IGUI_QNW_TaskSystem_Window_Progress") .. task:getProgress()
    local reward = getText("IGUI_QNW_TaskSystem_Window_Reward") .. task:getRewardText()
    self:drawText(time, 5, _y + FONT_HGT_LARGE + FONT_HGT_MEDIUM + 10, 1, 0, 0, 1, UIFont.NewSmall)
    if task:isComplete() then
        self:drawText(progress, 10 + getTextManager():MeasureStringX(UIFont.NewSmall, time), _y + FONT_HGT_LARGE + FONT_HGT_MEDIUM + 10, 0, 1, 0, 1, UIFont.NewSmall)
    else
        self:drawText(progress, 10 + getTextManager():MeasureStringX(UIFont.NewSmall, time), _y + FONT_HGT_LARGE + FONT_HGT_MEDIUM + 10, 1, 0, 0, 1, UIFont.NewSmall)
    end
    self:drawText(reward, 5, _y + FONT_HGT_LARGE + FONT_HGT_MEDIUM + FONT_HGT_SMALL + 15, 0, 1, 0, 1, UIFont.NewSmall)

    return _y + self.itemheight
end